<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="bbs.*,core.*,util.*" %>
<%@ page import="java.util.*" %>
<%@ include file="/WEB-INF/include/checkUser.jsp" %>
<!DOCTYPE html>
<html>
<head>
<%@ include file="/WEB-INF/include/header.jsp" %>
<style>
    form.comment { border-bottom: 1px dashed #ddd; padding: 10px 0 10px 10px; margin: 0; }
    form.comment > div:nth-of-type(1) { color: #aaa; font-size: 9pt; margin-bottom: 8px; }
    form.comment > div:nth-of-type(1) span { margin-right: 30px; }
    form.comment > div:nth-of-type(2) { white-space: pre-wrap; }
    textarea { ime-mode: active; width: 90%; min-height: 100px; }
    .edit, .add { display: none; }
    body { padding-bottom: 100px; }
</style>
</head>
<%
    RequestParameter param = new RequestParameter(request);
    int boardId = param.getInt("bid", 0);
    int articleId = param.getInt("aid", 0);
    String cmd = param.getString("cmd", "");
    int id = param.getInt("id", 0);
    String body = param.getString("body", "");
    String errMsg = null;

    Article article = ArticleDAO.selectById(articleId);
    BoardService boardService = new BoardService(request, boardId);
    if (boardService.canReadComment(article) == false)
        new UnauthorizedAccessException("덧글 보기 권한 없음");

    if (request.getMethod().equals("POST")) {
        if (boardService.canCreateComment() == false)
            new UnauthorizedAccessException("덧글 쓰기 권한 없음");
        if (cmd.equals("insert"))
            CommentDAO.insert(boardService.getCurrentUserId(), articleId, body);
        else {
            Comment comment = CommentDAO.selectById(id);
            if (boardService.canEditComment(comment) == false)
                new UnauthorizedAccessException("덧글 수정 권한 없음");
            if (cmd.equals("update"))
                CommentDAO.update(id, body);
            else if (cmd.equals("delete"))
                CommentDAO.delete(id);
        }
    }

    ArrayList<Comment> list = CommentDAO.select(articleId);

    String url = "commentList.jsp?aid=" + articleId;
%>
<script>
    function resize_parent_iframe() {
        if (window.parent) {
            var h = $("html").height();
            $('iframe[src*=comment]', window.parent.document).css("height", h);
        }
    }

    function readMode() {
        $(".read").show();
        $(".edit").hide();
        $(".add").hide();
    }

    function editMode(btn) {
        $(btn).parents("form").find(".read").hide();
        $(btn).parents("form").find(".edit").show();
        resize_parent_iframe();
    }

    function addMode() {
        $(".btn.read").hide();
        $(".add").show();
        resize_parent_iframe();
    }

    $(resize_parent_iframe);
</script>
<body>
    <% for (Comment comment : list) { %>
        <form class="comment" method="post">
            <input type="hidden" name="id" value="<%= comment.getId() %>">
            <div class="read">
                <span><%= comment.getName() %></span>
                <span><%= DateUtil.toYMDHMS(comment.getWriteTime()) %></span>
                <% if (boardService.canEditComment(comment)) { %>
                    <div class="pull-right">
                        <a class="btn btn-mini" onclick="editMode(this)">편집</a>
                        <button type="submit" class="btn btn-mini" name="cmd" value="delete"
                                onclick="return confirm('삭제하시겠습니까?')">삭제</button>
                    </div>
                <% } %>
            </div>
            <div class="read"><%= comment.getBody() %></div>
            <div class="edit">
                <textarea name="body"><%= comment.getBody() %></textarea>
                <div>
                    <button type="submit" class="btn btn-small" name="cmd" value="update">저장</button>
                    <a class="btn btn-small" onclick="readMode()">취소</a>
                </div>
            </div>
        </form>
    <% } %>

    <% if (boardService.canCreateComment()) { %>
        <div class="controls pull-right">
            <a onclick="addMode()" class="btn btn-small read">덧글 작성</a>
        </div>

        <form method="post" class="add">
            <textarea name="body"></textarea>
            <div>
                <button type="submit" class="btn btn-small" name="cmd" value="insert">저장</button>
                <a class="btn btn-small" onclick="readMode()">취소</a>
            </div>
        </form>
    <% } %>

</body>
</html>